package org.kl.bf.web.account;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.kl.bf.entity.basic.User;
import org.kl.bf.service.basic.CalendarService;
import org.kl.bf.service.basic.MenuService;
import org.kl.bf.service.basic.NoticeService;
import org.kl.bf.service.basic.NotifyInfoService;
import org.kl.bf.service.basic.UserService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.web.base.BasicController;
import org.kl.bpm.service.conf.BpmTaskConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 后台主页
 * 
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:34:36
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController extends BasicController {
	@Autowired
	private MenuService menuService;
	@Autowired
	private NotifyInfoService notifyInfoService;
	@Autowired
	private CalendarService calendarService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private BpmTaskConfService taskConfService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private UserService userService;
	


	protected static Map<String, ProcessDefinition> PROCESS_DEFINITION_CACHE = new HashMap<String, ProcessDefinition>();

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model, @RequestParam(value = "companyType", defaultValue = "") String companyType, HttpSession session) {

		model.addAttribute("sidebarMenus", menuService.getThreeLayerTreeMenu());
		model.addAttribute("notifyInfo", notifyInfoService.getNotReadReceiver(BasicConstants.ATTRIBUTE_UNREAD, BasicController.getCurrentUserId()));
		model.addAttribute("notifyInfosize", notifyInfoService.getCountNotReadReceiver(BasicController.getCurrentUserId()));
		model.addAttribute("noticeList", noticeService.getAllEnableNotice());
		
		User user=userService.getUser(BasicController.getCurrentUserId());
		if(StringUtils.isBlank(user.getPhone())){
			model.addAttribute("phoneFlag", true);//没有填写手机号
		}
		
		model.addAttribute("taskQuerysize", taskService.createTaskQuery().taskAssignee(BasicController.getCurrentUserId()).active().count());
		model.addAttribute("taskQuery", taskConfService.getEtaETasks());
		model.addAttribute("isSysManager", BasicController.isSysManager());

		model.addAttribute(
				"calendar",
				calendarService.getCalendarListBystartGreaterThan(new Timestamp(new DateTime().getMillis()),
						BasicController.getCurrentUserId()));
		model.addAttribute(
				"calendarsize",
				calendarService.getCalendarListBystartGreaterThan(new Timestamp(new DateTime().getMillis()),
						BasicController.getCurrentUserId()).size());
		
		
		return "account/index";
	}

}
